package com.example.blog.service;

import com.example.blog.constant.Constant;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.PageRequest;
import com.example.blog.model.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfo> getList() {
        List<BlogInfo> blogInfos = blogInfoMapper.selectAll();
        for (BlogInfo blog : blogInfos) {
            String content = blog.getContent()
                    .replaceAll("```[\\s\\S]*?```", "[代码块]")
                    .replaceAll("`[^`]*`", "[代码]")
                    .replaceAll("!\\[[^\\]]*\\]\\([^)]*\\)", "[图片]")
                    .replaceAll("\\[[^\\]]*\\]\\([^)]*\\)", "[链接]")
                    .replaceAll("#{1,6}\\s*", "")  // 更严格地匹配标题
                    .replaceAll("\\*\\*([^*]+)\\*\\*", "$1")  // 保留加粗文本内容
                    .replaceAll("\\*([^*]+)\\*", "$1")  // 保留斜体文本内容
                    .replaceAll(">\\s*", "")  // 移除引用标记
                    .replaceAll("\\s+", " ")
                    .trim();
            if (content.length() > Constant.MAX_LENGTH) {
                blog.setContent(content.substring(0,Constant.MAX_LENGTH));
            }else {
                blog.setContent(content);
            }
//            log.info("blog: {}",blog);
        }
        return blogInfos;
    }




    public BlogInfo getBlogDetail(Integer blogId) {
        return blogInfoMapper.selectById(blogId);
    }

    public Boolean insertBlog(BlogInfo blogInfo) {
        try {
            Integer result = blogInfoMapper.insertBlog(blogInfo);
            if (result == 1) {
                return true;
            }

        } catch (Exception e) {
            log.error("添加失败，e: ",e);
        }
        return false;
    }

    public Integer update(BlogInfo blogInfo) {
        Integer result = blogInfoMapper.updateBlog(blogInfo);
        return result;
    }

    public PageResponse<BlogInfo> getListByPage(PageRequest pageRequest) {
        PageResponse pageResponse = new PageResponse<>();
        List<BlogInfo> blogInfos = blogInfoMapper.getListByPage(pageRequest.getPageSize(),pageRequest.getOffSet());
        for (BlogInfo blog : blogInfos) {
            String content = blog.getContent()
                    .replaceAll("```[\\s\\S]*?```", "[代码块]")
                    .replaceAll("`[^`]*`", "[代码]")
                    .replaceAll("!\\[[^\\]]*\\]\\([^)]*\\)", "[图片]")
                    .replaceAll("\\[[^\\]]*\\]\\([^)]*\\)", "[链接]")
                    .replaceAll("#{1,6}\\s*", "")  // 更严格地匹配标题
                    .replaceAll("\\*\\*([^*]+)\\*\\*", "$1")  // 保留加粗文本内容
                    .replaceAll("\\*([^*]+)\\*", "$1")  // 保留斜体文本内容
                    .replaceAll(">\\s*", "")  // 移除引用标记
                    .replaceAll("\\s+", " ")
                    .trim();
            if (content.length() > Constant.MAX_LENGTH) {
                blog.setContent(content.substring(0, Constant.MAX_LENGTH));
            } else {
                blog.setContent(content);
            }
        }
        pageResponse.setList(blogInfos);
        Integer total = blogInfoMapper.count(null);
        pageResponse.setTotal(total);
        pageResponse.setPageRequest(pageRequest);
        return pageResponse;
    }
}
